import banner from '@/utils/world/banner.js'
import store from '../store'

/**
 * @param {string} 角色名称
 * @returns {string}
 */

export function getDefultPath(roleName) {
  let path = ""
  let time = new Date().getTime();
  let teamId = store.state.team.teamId ? store.state.team.teamId : ''
  if (!teamId || !roleName) {
    return path = "/user/create_team"
  } else {
    switch (roleName) {
      case 'visitor': //游客
        path = "/ad/overview/type-overview"
        break;
      case 'manager': //系列管理员
        path = "/ad/overview/type-overview"
        break;
      case 'finance': //财务
        path = "/finance/index"
        break;
      case 'readOnly': //账号只读
        path = "/ad/overview/type-overview"
        break;
      case 'campaignsReadOnly': //系列只读
        path = "/ad/overview/type-overview"
        break;
      case 'advManager': //广告管理员
        path = "/ad/overview/type-overview"
        break;
      default:
        path = "/ad/overview/type-overview"
        break;
    }
    return path + '?newTime=' + time
  }
}


export function isReadOnly(roleName) {
  let arr = ['readOnly', 'campaignsReadOnly'].map(item => {
    return item.toUpperCase()
  });
  let str = roleName.toUpperCase();
  let index = arr.indexOf(str);
  return index >= 0


}


// 随机生成字符串
export function getRandomStr (num){
  const chars = '0123456789abcdhijklmnopqrstuvwxyz'
  /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  const maxPos = chars.length
  const maxNum=num||8;
  let code = ''
  for (let i = 0; i < maxNum; i++) {
    code += chars.charAt(Math.floor(Math.random() * maxPos))
  }

}





/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
  if (!search) {
    return {}
  }
  const obj = {}
  const searchArr = search.split('&')
  searchArr.forEach(v => {
    const index = v.indexOf('=')
    if (index !== -1) {
      const name = v.substring(0, index)
      const val = v.substring(index + 1, v.length)
      obj[name] = val
    }
  })
  return obj
}


// 根据简称查找国家
export function findCountry(str) {
  let data = banner.zhCN;
  if (str.toUpperCase() === "GB") {
    str = 'uk'
  }
  let index = data.findIndex(item => {
    return str.toUpperCase() == item.icon.slice(str.length * -1).toUpperCase();
  })

  return data[index];
}


// 根据简称查找国家
export function findCountryByQuery(query, CNIsTrue) {
  let data = banner.zhCN;


  // 剪切成数组格式
  let arr;
  if (Array.isArray(query)) {
    arr = JSON.parse(JSON.stringify(query))
  } else if (query.indexOf(']') < 0) {
    arr = query.split(',')
  } else {
    let string1 = query.substring(1, query.length - 1);
    arr = string1.split(',');
  }




  // 循环得出数据
  let res = []

  arr.forEach(item => {
    let str = item.trim();
    let index = data.findIndex(v => {
      return str.toUpperCase() == v.icon.slice(str.length * -1).toUpperCase();
    })
    let obj = {
      ...data[index],
      short: item.trim().toUpperCase()
    }

    /* CNIsTrue 为true表示可以有中国的选项，为false表示不能有中国区的选项*/

    if (CNIsTrue) {
      res.push(obj)
    } else {
      if (item.toUpperCase() !== "CN") {
        res.push(obj)
      }

    }
  })
  distinguishHotOrNormal(res)
  return res;

}


// 将城市分为热门城市和全部城市
export function distinguishHotOrNormal(arr) {
  let res = [{
      label: '热门城市',
      options: []
    },
    {
      label: '全部城市城市',
      options: []
    },
  ]



}





// 计算环比值
export function returnRing(now, old) {

  // 如果上个周期是0
  if (old * 100 === 0) {
    if (now * 100 === 0) {
      // 如果这个周期也是0  那就是0增长
      return 0.00
    } else {
      // 如果上个周期为0  这个周期有数据则为+100%
      return 100.00
    }
  } else {
    // 如果上个周期数据不是0
    if (now * 100 === 0) {
      return (old * 100 * -1).toFixed(2)
    } else {
      const num = ((now * 100) - (old * 100)) / (now * 100)
      return (num * 100).toFixed(2)
    }
  }
}


// 计算平均值
export function returnAverage(num, list) {
  let total = num * 100;
  var length = undefined;
  if (list[0].date.length < 11) {

    length = list.length;
  } else {
    if (list.length < 24) {
      length = 1
    } else {
      length = list.length / 24
    }
  }

  let res = total / length / 100;
  return res.toFixed(2)
}

// 让数据千分位显示
export function formatNum(strNum) {
  if (strNum.length <= 3) {
    return strNum
  }
  if (!/^(\+|-)?(\d+)(\.\d+)?$/.test(strNum)) {
    return strNum
  }
  let a = RegExp.$1
  let b = RegExp.$2
  let c = RegExp.$3
  let re = new RegExp()
  re.compile("(\\d)(\\d{3})(,|$)")
  while (re.test(b)) {
    b = b.replace(re, '$1,$2$3')
  }
  return (a + '' + b + '' + c)
}
